﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MARZ_Script.analysis;
using MARZ_Script.node;

namespace SableCC15.CodeGeneration
{
    public class TemplateCodeGen : DepthFirstAdapter
    {
        private string _value;
        public override void CaseATempl(ATempl node)
        {
            //Let Template player Have
            //public class player {
            string id = node.GetId().ToString();
            Utility.Emit("public class " + id);
            node.GetHave().Apply(this);
            node.GetTemplateBody().Apply(this);
            node.GetEnd().Apply(new DeclarationCodeGen());
            Utility.Emit("}");
        }
        public override void CaseTHave(THave node)
        {
            Utility.Emit("{");
        }

        public override void CaseATemplateBody(MARZ_Script.node.ATemplateBody node)
        {
            node.GetTemplateBody().Apply(this);
            node.GetVarDecl().Apply(this);
        }

        public override void CaseADeclVarDecl(MARZ_Script.node.ADeclVarDecl node)
        {
            node.GetDecl().Apply(this);
        }

        public override void CaseAInitDeclVarDecl(MARZ_Script.node.AInitDeclVarDecl node)
        {
            _value = node.GetExpression().ToString().Trim();
            node.GetDecl().Apply(this);
            _value = null;
        }
        //TODO REMOVE THE UNNECCERSARY CODE
        public override void CaseADecl(MARZ_Script.node.ADecl node)
        {
            string type = node.GetType().ToString().Trim();
            if (type == "float")
                type = "double";
            string id = node.GetId().Text;
            //We can have variables or lists so..
            if (node.GetTList() != null)
            {
                Utility.Emit("List<" + type + "> " + id + " =" + " new List<" + type + ">" + "()");
                Utility.Emit(";");
            }
            if (_value == null)
            {
                Utility.Emit("public " + type + " " + id);
                Utility.Emit(";");
            }
            else
            {
                if (type.Equals("coord"))
                    Utility.Emit("public Coord " + id + " " + "=" + "new " + "Coord" + _value);
                else
                {
                    Utility.Emit("public " + type + " " + id + " " + "=" + _value);

                }
                Utility.Emit(";");
            }
        }
    }
}
